package com.vshop.framework.web.autoconfig;

import cn.dev33.satoken.exception.NotPermissionException;
import com.vshop.framework.web.handler.GlobalExceptionHandler;
import com.vshop.framework.web.handler.MybatisFlexExceptionHandler;
import com.vshop.framework.web.handler.SaTokenExceptionHandler;
import com.vshop.framework.web.log.WebLogTimeAspect;
import jakarta.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.MyBatisSystemException;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.dao.DuplicateKeyException;

@Slf4j
@AutoConfiguration
public class WebAutoConfig {
    @PostConstruct
    public void postConstruct() {
        log.info("初始化 WebAutoConfig");
    }

    @Bean
    public WebLogTimeAspect webLogTimeAspect() {
        return new WebLogTimeAspect();
    }

    @Bean
    public GlobalExceptionHandler globalExceptionHandler() {
        log.info("初始化 GlobalExceptionHandler");
        return new GlobalExceptionHandler();
    }

    @Bean
    @ConditionalOnClass(value = {MyBatisSystemException.class, DuplicateKeyException.class})
    public MybatisFlexExceptionHandler mybatisFlexExceptionHandler() {
        log.info("初始化 MybatisFlexExceptionHandler");
        return new MybatisFlexExceptionHandler();
    }

    @Bean
    @ConditionalOnClass(value = {NotPermissionException.class})
    public SaTokenExceptionHandler saTokenExceptionHandler() {
        log.info("初始化 SaTokenExceptionHandler");
        return new SaTokenExceptionHandler();
    }
}
